import axios from 'axios'
import qs from 'qs'

const baseURL = 'http://127.0.0.1:5172'

// 创建 axios 实例
const instance = axios.create({
  baseURL: baseURL,
  paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'repeat' })
})

// 请求拦截器
instance.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    if (response?.status === 200) {
      return response.data
    }
    return Promise.reject(response.data)
  },
  (error) => {
    ElMessage.error(error.response.data)
    return Promise.reject(error)
  }
)

export default instance
